<?php

namespace app\mall\controller\api\uc\goods;

use app\mall\exceptions\ApiException;
use app\mall\logic\order\OrderUserLogic;
use app\mall\model\MallGoodsStock;

class Buy extends Base
{

    /**
     * @return \think\response\Json
     * @throws \app\mall\exceptions\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $stockId = $this->request->post('stock_id', null, 'intval');
        $num = $this->request->post('num', 1, 'intval');
        $addressId = $this->request->post('address_id', 1, 'intval');
        $storeId = $this->request->post('store_id', 1, 'intval');
        if (!$stockId || !$num || !$addressId) {
            ApiException::throws(ApiException::$requireArgs);
        }
        /** @var MallGoodsStock $stock */
        $stock = MallGoodsStock::find($stockId);
        if (!$stockId) {
            ApiException::throws(ApiException::$infoNotFound);
        }
        $mallUser = $this->getMallUser();
        $rs = OrderUserLogic::getInstance()->create(
            $mallUser,
            $stock,
            $num,
            $addressId,
            $storeId
        );
        return json_return($rs);
    }

}